package com.finance.framework.parser;
import com.finance.rule.computer.IMajorAnalyzer;
import com.finance.rule.computer.ParserFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 菜单注册
 * 扫描所有@MenuAware注解的菜单URL 加入到系统的MenuFactory单实例中
 */
@Component
 public  class ParserAwareBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
     private ApplicationContext applicationContext;

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        Class[] interfaces = o.getClass().getInterfaces();
        if(interfaces != null && interfaces.length > 0){
            for (Class i : interfaces){
                if(i == IMajorAnalyzer.class){
                    ParserFactory.getIntance().add(o.getClass().getName());
                }
            }
        }
        return o;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}